package com.licheng.controller;

import com.licheng.pojo.Result;
import com.licheng.utils.AliyunOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@Slf4j

public class UploadController {
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @PostMapping("/upload")
    public Result upLoad(MultipartFile file) throws Exception {
        //获取源文件名
        Resource resource = file.getResource();
        //把源文件名包装成一个不会重名的文件名
        String newFile = System.currentTimeMillis() + "_" + UUID.randomUUID() + "_" + resource;
        String url = AliyunOSSUtils.upload(endpoint, bucketName, file.getBytes(), newFile);
        return Result.success(url);
    }
}
